﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Data.Services.Client;

namespace FeedReader.Shared.Data
{
    public class FeedsDataSource
    {
        private FeedsDataServiceContext _ServiceContext = null;

        public FeedsDataSource(CloudStorageAccount storageAccount)
        {
            //var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            _ServiceContext = new FeedsDataServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials);

            // Create the tables
            // In this case, just a single table.  
            storageAccount.CreateCloudTableClient().CreateTableIfNotExist(FeedsDataServiceContext.FeedsTableName);
        }
        public IEnumerable<FeedsDataModel> Select(string name)
        {
            var results = from c in _ServiceContext.FeedsTable
                          where c.BlogID == name
                          select c;

            var query = results.AsTableServiceQuery<FeedsDataModel>();
            var queryResults = query.Execute();

            return queryResults;
        }
        //public IEnumerable<FeedsDataModel> SelectTitle(string name)
        //{
        //    var results = from c in _ServiceContext.FeedsTable
        //                  where c.Title == name
        //                  select c;

        //    var query = results.AsTableServiceQuery<FeedsDataModel>();
        //    var queryResults = query.Execute();

        //    return queryResults;
        //}
        //public IEnumerable<FeedsDataModel> Select()
        //{
        //    var results = from c in _ServiceContext.FeedsTable
        //                  select c;

        //    var query = results.AsTableServiceQuery<FeedsDataModel>();
        //    var queryResults = query.Execute();

        //    return queryResults;
        //}
        //public IEnumerable<FeedsDataModel> SelectBlobs(bool rssenabled)
        //{
        //    var results = from c in _ServiceContext.FeedsTable
        //                  where c.RSSEnabled ==rssenabled
        //                  select c;

        //    var query = results.AsTableServiceQuery<FeedsDataModel>();
        //    var queryResults = query.Execute();
            
        //    // this step may not be required
            
        //    return queryResults;
        //}
        //public IEnumerable<FeedsDataModel> SelectRange(IEnumerable<String> items)
        //{
        //    var results = Select();

        //    var queryResults = from d in results
        //                       where items.Contains(d.BlogID)
        //                       select d;
                                
        //    // this step may not be required

        //    return queryResults;
        //}

        //public void Delete(FeedsDataModel itemToDelete)
        //{
        //    _ServiceContext.AttachTo(FeedsDataServiceContext.FeedsTableName, itemToDelete, "*");
        //    _ServiceContext.DeleteObject(itemToDelete);
        //    _ServiceContext.SaveChanges();
        //}
        //public void DeleteRange(IEnumerable<FeedsDataModel> itemsToDelete)
        //{
        //    foreach (FeedsDataModel itemToDelete in itemsToDelete)
        //    {
        //        _ServiceContext.AttachTo(FeedsDataServiceContext.FeedsTableName, itemToDelete, "*");
        //        _ServiceContext.DeleteObject(itemToDelete);
        //        _ServiceContext.SaveChanges();
        //    }
        //}

        //public void Insert(FeedsDataModel newItem)
        //{
        //    _ServiceContext.AddObject(FeedsDataServiceContext.FeedsTableName, newItem);
        //    _ServiceContext.SaveChanges();
        //}

        //public void Update(FeedsDataModel item)
        //{
        //    _ServiceContext.AttachTo(FeedsDataServiceContext.FeedsTableName, item, "*");
        //    _ServiceContext.UpdateObject(item);
        //    _ServiceContext.SaveChanges();

        //}
        //public void SaveChanges()
        //{
        //    _ServiceContext.SaveChanges();
            
        //}
        //public System.Data.Services.Client.MergeOption MergeOption
        //{
        //    get
        //    {
        //        return _ServiceContext.MergeOption;
        //    }
        //    set
        //    {
        //        _ServiceContext.MergeOption = value;
        //    }
        //}

        public void Insert(FeedsDataModel dataitem)
        {
            _ServiceContext.AddObject(FeedsDataServiceContext.FeedsTableName, dataitem);
            _ServiceContext.SaveChanges();
        }

        public object Select()
        {
            var results = from c in _ServiceContext.FeedsTable
                          select c;

            var query = results.AsTableServiceQuery<FeedsDataModel>();
            var queryResults = query.Execute();

            return queryResults;
        }
    }
}
